package com.aids_malaria.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public abstract class BaseQuickAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {

    public Context mContext;
    public int mLayoutResID;
    public List<T> mData;
    public OnItemClickListener mOnItemClickListener = null;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public BaseQuickAdapter(Context context, int layoutResID, List<T> data) {
        mContext = context;
        this.mLayoutResID = layoutResID;
        this.mData = data;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final BaseViewHolder holder;
        final View itemView = LayoutInflater.from(mContext).inflate(mLayoutResID, parent, false);
        holder = new BaseViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(null != mOnItemClickListener){
                    mOnItemClickListener.onItemClick(position);
                }
            }
        });
        convert(holder, mData.get(position));
    }

    public abstract void  convert(BaseViewHolder holder, T item);

    @Override
    public int getItemCount() {
        return mData.size();
    }

    //回调接口
    public interface OnItemClickListener {
        void onItemClick(int position);
    }

}
